/**
 *******************************************************************************
 * @brief       Provide a brief description of the module here (Single line).
 * 
 * @author      Ashwin Vijayakumar
 * 
 * @file        McalIsr.c
 * 
 * @version
 * 
 * @date        Aug 18, 2012 4:29:00 PM
 * 
 * @details     Provide a detailed description of the module here (Multi-line).
 * 
 * @pre         Mention any preconditions required to use this module.
 * 
 * @bug         List any bugs in this module.
 * 
 * @note        List any odd modes while using this module.
 * 
 * @warning     List any warnings against using the objects of this module.
 *******************************************************************************
 * @copyright 2012
 * <a href="http://www.afflatustech.com">Afflatus Techtronics</a>
 * and <a href="http://www.ashwinvijayakumar.co.nr">Ashwin Vijayakumar</a>
 * @n@n
 * This work is licensed under the Creative Commons
 * Attribution-NonCommercial-ShareAlike 3.0 Unported License. To view a copy
 * of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or
 * send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain
 * View, California, 94041, USA.
 *******************************************************************************
 */

/**@cond -----------------------------------------------------------------------
 * Includes section
 * Add all #include here.
 * ----------------------------------------------------------------- @endcond */

/**@cond System headers -------------------------------------------- @endcond */
#include <p18cxxx.h>
#include <stdlib.h>
#include <string.h>
#include <EEP.h>

/**@cond Local headers --------------------------------------------- @endcond */
#include "McalIsr.h"
#include "McalUart.h"
#include "PalDs1629.h"
#include "CalAtCmd.h"
#include "TimeWp.h"
#include "PrintWp.h"
#include "time.h"
#include "EEP.h"

/**@cond -----------------------------------------------------------------------
 * typedef section (File scope)
 * Add typedef's with file scope here.
 * Capitalization: Upper_Camel_Case.
 * Declare typedef's with project scope in McalIsr.h.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Constants section (File scope)
 * Add all constants with file scope here.
 * Capitalization: ALL_CAPS.
 * Note that all constants listed here are limited to this file scope.
 * For project wide scope, declare constants in McalIsr.h.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Macros section (File scope)
 * Add all macros with file scope here.
 * Capitalization: ALL_CAPS.
 * Note that all macros listed here are limited to this file scope.
 * For project wide scope, declare macros in McalIsr.h.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Global variables section (File scope)
 * Add global variables with file scope here. 
 * Capitalization: lower_case.
 * Declare them with 'static' keyword.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Global variables section (Project scope)
 * PROJECT SCOPE G-VARIABLES ARE LIKE FORBIDDEN FRUIT. REFRAIN FROM USING THEM, 
 * DECLARE FILE SCOPE G-VARIABLES INSTEAD AND DEFINE GETTER/SETTER METHODS TO 
 * ACCESS THEM.
 * Add global variables with project scope here. 
 * Capitalization: lower_case.
 * Declare them without 'static' keyword.
 * Also declare these variables in McalIsr.h with 'extern' keyword.
 * ----------------------------------------------------------------- @endcond */

volatile uint8_t RxBuffer[10];
volatile uint8_t RxDoubleBuffer[10];

/**@cond -----------------------------------------------------------------------
 * Function prototypes section (File scope)
 * Add prototypes for all functions with file scope here. 
 * Capitalization: Upper_Camel_Case.
 * Declare them with 'static' keyword.
 * For functions with project scope, declare the function prototype in 
 * McalIsr.h without 'static' keyword.
 * ----------------------------------------------------------------- @endcond */

/**@cond -----------------------------------------------------------------------
 * Getter/setter method section (Project scope)
 * Define getter/setter methods to provide project scope global access to file 
 * scope global variables. (AVOID USING THESE IF YOU HAVE THROUGHPUT ISSUES).
 * Capitalization: get_Camel_Case/is_Camel_Case/set_Camel_Case.
 * Declare the prototype for these functions in McalIsr.h. 
 * ----------------------------------------------------------------- @endcond */

/**@cond ***********************************************************************
 * TODO: Update all comment fields and delete this line.
 * MOD: For functions with project scope, remove 'static' keyword and declare
 * prototype in McalIsr.h
 * -------------------------------------------------------------------- @endcond
 * @fn          return_type function_name(TYPE parameter1, TYPE parameter2)
 * @return      Describe the function's return value here.
 * @param       parameter1 Describe the function's 1st parameter here.
 * @param       parameter2 Describe the function's 2nd parameter here.
 * @author      avijayak
 * @date        Aug 18, 2012, 4:34:36 PM
 * @brief	Provide a brief description of the module here (Single line).
 * @details	Provide a detailed description of the module here (Multi-line).
 * @pre         Mention any preconditions required to use this function.
 * @post        Mention any postcondition required to use this function.
 * @bug         List any bugs in this module.
 * @note        List any odd modes while using this function.
 * @warning     List any warnings against using the objects of this function.
 *******************************************************************************
 */

#pragma interrupt McalIsr_High
void
McalIsr_High( void )
{
  /* Disable all high priority interrupts */
  INTCONbits.GIEH = 0;

  /* Rx interrupt flag: Did UART Rx cause the interrupt? */
  if ( PIR1bits.RCIF )
    {
      McalIsr_UartRxIsr( (volatile uint8_t)RCREG );
    }
  /* Add else if for other interrupt flags here */

  /* Enable all high priority interrupts */
  INTCONbits.GIEH = 1;
}

/** End of McalIsr_High ************* Copyright (C) 2012 Afflatus Techtronics */

/**@cond ***********************************************************************
 * TODO: Update all comment fields and delete this line.
 * MOD: For functions with project scope, remove 'static' keyword and declare
 * prototype in McalIsr.h
 * -------------------------------------------------------------------- @endcond
 * @fn          return_type function_name(TYPE parameter1, TYPE parameter2)
 * @return      Describe the function's return value here.
 * @param       parameter1 Describe the function's 1st parameter here.
 * @param       parameter2 Describe the function's 2nd parameter here.
 * @author      avijayak
 * @date        Aug 18, 2012, 4:57:39 PM
 * @brief	Provide a brief description of the module here (Single line).
 * @details	Provide a detailed description of the module here (Multi-line).
 * @pre         Mention any preconditions required to use this function.
 * @post        Mention any postcondition required to use this function.
 * @bug         List any bugs in this module.
 * @note        List any odd modes while using this function.
 * @warning     List any warnings against using the objects of this function.
 *******************************************************************************
 */

void
McalIsr_UartRxIsr( volatile uint8_t RxReg )
{
  static uint8_t RxCounter = 0;
  CalAtCmd_CmdSetType tempAtCmd = AT_CMD_ILLEGAL;
  char msgBuf;
  int eepPtr = 0;

  /* Echo received character */
  McalUart_Putc( RxReg );

  /* Accumulate characters until carriage return is received */
  if( RxReg != '\r' )
    {
      RxBuffer[RxCounter++] = RxReg;
    }
  else
    {
      /* NULL terminate the Rx buffer */
      RxBuffer[RxCounter] = '\0';

      /* Reset receive counter */
      RxCounter = 0;

      /* Parse the received command */
      tempAtCmd = CalAtCmd_ParseCmd( (char*)RxBuffer );

      switch( tempAtCmd )
      {
      case AT_CMD_ATTENTION:
        PrintWp_UiMsgRom( "\n\rOK\n\r", PRINT_DEVICE_ID_UART0 );
        break;

      case AT_CMD_SREG_TIME:
        if( E_OK == TimeWp_SetTimeUart() )
          {
            PrintWp_UiMsgRom( "\n\rOK\n\r", PRINT_DEVICE_ID_UART0 );
          }
        break;

      case AT_CMD_SREG_DATE:
        if( E_OK == TimeWp_SetDateUart() )
          {
            PrintWp_UiMsgRom( "\n\rOK\n\r", PRINT_DEVICE_ID_UART0 );
          }
        break;

      case AT_CMD_SREG_MSG:
        PrintWp_UiMsgRom( "\n\rEnter message and hit ENTER\n\r", PRINT_DEVICE_ID_UART0 );

        /* Poll Data EEPROM until a empty byte space if found. */
        eepPtr = 0;
        do
          {
            Busy_eep();
            msgBuf = Read_b_eep( eepPtr++ );
          } while( 0xFF != msgBuf );

        /* Accept and write data to Data EEPROM */
        eepPtr--;
        do
          {
            msgBuf = McalUart_Getc();
            McalUart_Putc(msgBuf);
            Busy_eep();
            Write_b_eep( eepPtr++, msgBuf );
            /* Disable all high priority interrupts (Doing this again since the
             * C18 EEP library enables it after the write. */
            INTCONbits.GIEH = 0;
          } while( '\r' != msgBuf );

        PrintWp_UiMsgRom( "\n\rOK\n\r", PRINT_DEVICE_ID_UART0 );

        break;

      case AT_CMD_ILLEGAL:
        McalUart_Putrs( "\n\rMcalIsr_UartRxIsr: Illegal AT command" );
        break;
      } /* switch( tempAtCmd ) */
    }

  /* Reset the Rx interrupt flag */
  PIR1bits.RCIF = 0;
}

/** End of McalIsr_UartRxIsr ******** Copyright (C) 2012 Afflatus Techtronics */

/** END OF FILE ********************* Copyright (C) 2012 Afflatus Techtronics */
